//
//  ST_DTChatIncomeGiftCell.swift
//  btvideo
//
//  Created by mac on 2020/10/22.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift


class ST_DTChatIncomeGiftCell: ST_DTChatCell {
    
    static let height = (75+15).cgFloat
    
    fileprivate lazy var box_view:UIView = {
       let control = UIView()
        control.backgroundColor = .white
        control.borderColor = UIColor.init(hex: 0xF2F4F7)
        control.borderWidth = 1
        control.cornerRadius = 20
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(click_goods))
        
        control.addGestureRecognizer(tap)
        
        return control
    }()
    
    @objc fileprivate func click_goods(){
        
        
        guard let goods = message.goods else{
            return
        }
        
        if goods.type == .gift{
            
            ST_GiftAnimationViewController.browserGift(imageUrl: goods.imageUrl, svgaUrl: goods.svgaUrl)
            
            
            return
        }else if goods.type == .sweetGuard{
            
            let animateVC = MemberGoodsAnimationViewController()
            
            animateVC.overCurrentContext = true
            
            animateVC.over_current_context_alpha = "0.4"
            
            var avatarStr = ""
            
            
            
            if message.direction == .send{
                
                if let info = BINP.fetch_nickname_with_avatar(userId: message.chatID){
                    
                    avatarStr = info.avatar
                }
            }else{
                if let url = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.avatar{
                    avatarStr = url
                }
            }
            
            animateVC.param = (avatar:avatarStr,supplementUrl:goods.imageUrl)
            
            GlobalPresent(toController: animateVC, animated: false)
            
            return
        }
        
        if goods.type == .requireDetect,message.direction == .receive {
            
            if FACE_AVATAE_STATE == 2{
          
            let failure = ST_MatchFaceFailureController()
            
            failure.overCurrentContext = true
            
            GlobalPresent(toController: failure)
            
            return
            
            }else if FACE_AVATAE_STATE != 3{
                DetectFaceHandle()
            }
            
        }
        
    }
    
    fileprivate lazy var goods_image_view:UIImageView = {
       let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.clipsToBounds = true
        return control
    }()
    
    fileprivate lazy var goods_title_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var goods_sub_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "打开看看"
        control.textColor = UIColor.init(hex: 0x42CEDA)
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var goods_num_label:UILabel = {
        let control = UILabel.init()
        control.alpha = 0
        return control
    }()

    
    override var message: ST_DTChatMessage!{
        didSet{
            
            if uuid == message.uuid{
                return
            }
            
            self.uuid = message.uuid
            
            icon.sd_setImage(with: message.portraitUri.compress_image().url,for: UIControl.State.normal, completed: nil)
            
            guard let goods = message.goods else { return  }
            
            goods_title_label.text = goods.title
            
            if message.direction == .send{
                goods_sub_title_label.text = "打开看看"
            }else{
                goods_sub_title_label.text = goods.subTitle
            }
            
          
            goods_image_view.pin_setImage(from: goods.imageUrl.url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            
            if let str = message.giftNumberAttributeString{
                goods_num_label.attributedText = str
                goods_num_label.alpha = 1
            }else{
                goods_num_label.alpha = 0
            }
            
            
            
            
        }
    }
}




//MARK: - Left

class ST_DTChatIncomeGiftLeftCell: ST_DTChatIncomeGiftCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func commonInit() {
        super.commonInit()
        
        icon.snp.makeConstraints { (make) in
            make.width.height.equalTo(DTChatUserImageWidth)
            make.left.top.equalToSuperview().offset(15)
        }
        
        contentView.addSubview(box_view)
        box_view.snp.makeConstraints { (make) in
            make.width.equalTo(207)
            make.height.equalTo(75)
            make.left.equalTo(icon.snp.right).offset(10)
            make.top.equalTo(icon)
        }
        
        box_view.addSubview(goods_image_view)
        goods_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(55)
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
        }
        
        box_view.addSubview(goods_title_label)
        goods_title_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(18)
            make.height.equalTo(20)
        }
        
        box_view.addSubview(incom_image_view)
        incom_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(14)
            make.top.equalTo(goods_title_label.snp.bottom).offset(7)
            make.left.equalTo(goods_title_label)
        }
        
        box_view.addSubview(goods_sub_title_label)
        goods_sub_title_label.snp.makeConstraints { (make) in
            make.height.equalTo(14)
//            make.top.equalTo(goods_title_label.snp.bottom).offset(5)
            make.centerY.equalTo(incom_image_view)
            make.left.equalTo(incom_image_view.snp.right).offset(5)
        }
        
        box_view.addSubview(check_icon_image_view)
        check_icon_image_view.snp.makeConstraints { (make) in
            make.width.equalTo(13)
            make.height.equalTo(13)
            make.left.equalTo(goods_sub_title_label.snp.right).offset(2)
            make.centerY.equalTo(goods_sub_title_label)
        }
        
        
        
        contentView.addSubview(goods_num_label)
        goods_num_label.snp.makeConstraints { (make) in
            make.left.equalTo(box_view.snp.right).offset(10)
            make.centerY.equalTo(box_view)
        }
        /*
        box_view.addSubview(representGiftBtn)
        representGiftBtn.snp.makeConstraints { (make) in
            make.width.equalTo(50)
            make.height.equalTo(18)
            make.left.equalTo(goods_sub_title_label.snp.right).offset(5)
            make.centerY.equalTo(goods_sub_title_label)
        }*/
        
    }
    
    /*
    fileprivate lazy var representGiftBtn:UIButton = {
       
       let control = UIButton.init(type: UIButton.ButtonType.custom)
       control.backgroundColor = UIColor.init(hex: 0x42CEDA)
       control.cornerRadius = 18/2.0
       control.setTitleForAllStates("回个礼")
       control.setTitleColorForAllStates(UIColor.white)
       control.titleLabel?.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
       control.addTarget(self, action: #selector(represent_gift_handle), for: UIControl.Event.touchUpInside)
       return control
   }()*/
    
    fileprivate lazy var incom_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("privateChatIncomeIcon"))
              control.contentMode = .scaleAspectFill
              return control
          }()
    
    fileprivate lazy var check_icon_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("打钩路径"))
          control.contentMode = .scaleAspectFill
        control.isHidden = true
          return control
    }()
    
    
    override var message: ST_DTChatMessage!{
        didSet{
            
            if message.tail.titleStr.count < 1{
                goods_sub_title_label.text = "回复提升亲密度"
            }else{
                goods_sub_title_label.text = message.tail.titleStr
            }
            
            check_icon_image_view.isHidden = message.tail.status != .replied
            
//            let offset = check_icon_image_view.isHidden ? 5 : 20
            
//            representGiftBtn.snp.updateConstraints { (make) in
//                make.left.equalTo(goods_sub_title_label.snp.right).offset(offset)
//            }
        }
    }
    
    // 礼物 🎁
    var present_goods_closure:(()->())?

    @objc fileprivate func represent_gift_handle(){
        
        self.present_goods_closure?()
        
    }
}


//MARK: - Right
class ST_DTChatIncomeGiftRightCell: ST_DTChatIncomeGiftCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func commonInit() {
        super.commonInit()
    
        contentView.addSubview(box_view)
        box_view.snp.makeConstraints { (make) in
            make.width.equalTo(207)
            make.height.equalTo(75)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(15)
        }
        
        box_view.addSubview(goods_image_view)
        goods_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(55)
            make.left.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
        }
        
        box_view.addSubview(goods_title_label)
        goods_title_label.snp.makeConstraints { (make) in
            make.left.equalTo(goods_image_view.snp.right).offset(10)
            make.top.equalToSuperview().offset(18)
            make.height.equalTo(20)
        }
        
        box_view.addSubview(goods_sub_title_label)
        goods_sub_title_label.snp.makeConstraints { (make) in
            make.height.equalTo(14)
            make.top.equalTo(goods_title_label.snp.bottom).offset(5)
            make.left.equalTo(goods_title_label)
        }
        
        contentView.addSubview(upload_error_btn)
        upload_error_btn.snp.makeConstraints { (make) in
            make.centerY.equalTo(box_view)
            make.right.equalTo(box_view.snp.left).offset(-8)
            make.width.height.equalTo(16)
        }
        
        contentView.addSubview(uploadIndicator)
        uploadIndicator.snp.makeConstraints { (make) in
            make.centerY.equalTo(box_view)
            make.right.equalTo(box_view.snp.left).offset(-8)
        }
    }
    
   
    override func setting_uplaod_status() {
        
        if message.uploadStatus == .uploading {
            
            uploadIndicator.startAnimating()
            
        }else{
            uploadIndicator.stopAnimating()
        }
        
        upload_error_btn.isHidden = message.uploadStatus != .failure
    }
}
